package Frame.shopSet;

import FCRendering.FCRMaster;
import FCRendering.sw.FCRValues;
import Frame.entity.UnifyShopSayUIEntity;
import VWorldGameStudio.FCItem;
import VWorldGameStudio.FCPlayerInfo;
import VWorldGameStudio.FCRPlayerLink;
import VWorldGameStudio.FCTank;
import Function.FCMutilPlayCache;
import VWorldGameStudio.events.ShopEvent;

import java.util.Vector;

import static FCRendering.FCRMaster.*;

/**
 * 的商店点击事件和显示都在这里做了
 */
public class FunctionCommonShop extends AShopFuntion {
    private Vector<String> itemNames;
    private Vector<Integer> as = new Vector<>();
    private int showFristIndex = 0;

    private int endI = 0;

    private int willBuyPerPrice = 0;
    private String willBuyItemName = "null";

    private void setEndIndex(int num){
        endI = num;
    }

    public FunctionCommonShop(){
        buildShopWindow();
    }


    public void buildShopWindow() {
        itemNames = ((ShopEvent)fcv.getPlayingEvent()).getItemid();
        for(String name: itemNames) {
            as.add(itemLib.getItemOfID(name).getBuyInPrice());
        }
        setEndIndex(Math.min(itemNames.size(), showFristIndex + 7));
       resetUI();
    }

    private void resetUI(){
        FCRValues.resetUnifySayUIEntity();
        int m = 0;
        for (int i = showFristIndex; i < endI; i++) {
            FCRValues.leftWindowUI[m].leftText = itemLib.getItemOfID(itemNames.get(i)).getChName();
            FCRValues.leftWindowUI[m].rightText = as.get(i) + "G";
            m++;
        }

        FCRValues.rightWindowUI[1].leftText = "T:购买物品自动放入背包中";
        FCRValues.rightWindowUI[3].rightText = "上一页";
        FCRValues.rightWindowUI[4].rightText = "下一页";
        FCRValues.rightWindowUI[5].rightText = "购买";
        FCRValues.rightWindowUI[6].rightText = "不想买了";
    }

    @Override
    public void clickLeftWindow(int i, UnifyShopSayUIEntity entity) {
        //永远都是0到6
        if (entity.leftText.length() > 1) {
            FCItem item = itemLib.getItemOfID(itemNames.get(showFristIndex + i));
            if (item != null) {
                FCRValues.rightWindowUI[0].leftText = item.getDepict();
                willBuyPerPrice = as.get(showFristIndex + i);
                willBuyItemName = item.getName();
                FCRValues.rightWindowUI[1].leftText = "点击购买"+item.getChName();
                System.out.println("点击购买"+item.getChName()+" "+item.getName());
            }
        }
    }

    @Override
    public void clickRightWindow(int i, UnifyShopSayUIEntity entity) {
        setEndIndex(Math.min(itemNames.size(), showFristIndex + 7));//保留这句代码
         switch (i) {
            case 3:
                if (showFristIndex >= 7) {
                    showFristIndex -= 7;
                    setEndIndex(Math.min(itemNames.size(), showFristIndex + 7));
                    resetUI();
                }
                break;
            case 4:
                if (endI < itemNames.size()) {
                    showFristIndex += 7;
                    setEndIndex(Math.min(itemNames.size(), showFristIndex + 7));
                    resetUI();
                }
                break;
            case 5:
                if(willBuyItemName != null) {
                    buyItWithMoney(willBuyPerPrice);
                }else {
                    FCRValues.rightWindowUI[1].leftText = "T:请先点击选择物品";
                }
                break;
            case 6://取消
                fcv.setOpenSayShop(false);
                FCRMaster.fcv.setOpenSay(false);
                FCRMaster.fcv.setSayNpcName(null);
                FCRMaster.fcv.setSayNpcTextNum(0);
                break;
        }
    }

    private boolean buyInTank(FCItem item, int needMoney, int mode) {
        FCTank tank = tankLib.getTankOfID(player1.getPlayertank1(), player1.getPlayertankHostId());
        boolean hasTank = false;
        if (tank != null) {
            hasTank = true;
            for (int i = 0; i < 10; i++) {
                if (tank.getKnapsack()[i] == null || tank.getKnapsack()[i].equals("null")) {
                    tank.insertItemToKnapsack(i, willBuyItemName);
                    player1.setMoney(player1.getMoney() - needMoney);
                    tank.reInitShuxing();
                    FCRValues.rightWindowUI[1].leftText = "T:物品已放到背包，请继续";
                    return true;
                }
            }
        }
        for (FCRPlayerLink link : FCMutilPlayCache.getTeamMembers()) {
            if (link.curPlayerHostUID.equals(gameSave.HOST_UID)) {
                FCPlayerInfo p = FCMutilPlayCache.findPlayerByName(gameSave.HOST_UID, link.curPlayerName);
                tank = tankLib.getTankOfID(p.getPlayertank1(), p.getPlayertankHostId());
                if (tank != null) {
                    hasTank = true;
                    for (int i = 0; i < 10; i++) {
                        if (tank.getKnapsack()[i] == null || tank.getKnapsack()[i].equals("null")) {
                            tank.insertItemToKnapsack(i, willBuyItemName);
                            player1.setMoney(player1.getMoney() - needMoney);
                            tank.reInitShuxing();
                            FCRValues.rightWindowUI[1].leftText = "T:物品已放到背包，请继续";
                            return true;
                        }
                    }
                }
            }
        }
        if(mode == 1) {
            if (!hasTank) {
                FCRValues.rightWindowUI[1].leftText = "T:你的战车呢？";
            } else {
                FCRValues.rightWindowUI[1].leftText = "T:战车已没多余空间";
            }
        }
        return false;
    }

    private void buyItWithMoney(int needMoney) {
        if (player1.getMoney() >= needMoney) {
            //检查有没有空背包
            FCItem item = itemLib.getItemOfID(willBuyItemName);
            if (item.getCatalog().equals("tank")) {
                //坦克道具
                if(buyInTank(item, needMoney, 1)){

                }
            } else {
                for (int i = 0; i < 10; i++) {
                    if (player1.getKnapsack()[i] == null || player1.getKnapsack()[i].equals("null")) {
                        player1.insertItemToKnapsack(i, willBuyItemName);
                        player1.setMoney(player1.getMoney() - needMoney);
                        FCRValues.rightWindowUI[1].leftText = "T:物品已放到背包，请继续";
                        return;
                    }
                }
                for (FCRPlayerLink link : FCMutilPlayCache.getTeamMembers()) {
                    if (link.curPlayerHostUID.equals(gameSave.HOST_UID)) {
                        FCPlayerInfo p = FCMutilPlayCache.findPlayerByName(gameSave.HOST_UID, link.curPlayerName);
                        for (int i = 0; i < 10; i++) {
                            if (p.getKnapsack()[i] == null || p.getKnapsack()[i].equals("null")) {
                                p.insertItemToKnapsack(i, willBuyItemName);
                                player1.setMoney(player1.getMoney() - needMoney);
                                FCRValues.rightWindowUI[1].leftText = "T:物品已放到背包，请继续";
                                return;
                            }
                        }
                    }
                }
                if(!buyInTank(item, needMoney, 2)) {
                    FCRValues.rightWindowUI[1].leftText = "T:你已经没有多余的空间";
                }else {

                }
            }
        } else {
            FCRValues.rightWindowUI[1].leftText = "T:金币不足!";
        }
    }
}
